﻿using Org.BouncyCastle.Crypto;
using ProTrack.Entity;
using ProTrack.IRepository;
using ProTrack.IServices.Works;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace ProTrack.Services.Works
{
    public class WorkstationServices : IWorkstationServices
    {
        public WorkstationServices(IBaseRepository db)
        {
            Db = db;
        }

        public IBaseRepository Db { get; }

        public async Task<bool> AddAsync(Workstation model, List<WorkstationMachine> list)
        {
            var result = await Db.InsertIdentityTranAsync(model, list);
            return result;
        }

        public async Task<bool> DeleteAsync(int[] Ids)
        {
            string sql = "delete Workstation where Id in @Ids";
            var result = await Db.DeleteAsync(sql,new { Ids });
            return result;
        }

        public async Task<bool> DeleteAsync(int Id)
        {
            string sql = "delete Workstation where Id = @Id;delete WorkstationMachine where WorkstationId =@Id;";
            var result = await Db.DeleteAsync(sql, new { Id });
            return result;
        }

        public async Task<IEnumerable<Workstation>> GetAllAsync()
        {
            string sql = "select * from Workstation";
            var list = await Db.QueryAllAsync<Workstation>(sql);
            return list;
        }

        public async Task<IEnumerable<Workstation>> GetAsync(int PageIndex, int PageSize, string WorkstationCode, string WorkstationName,string WorkshopId, string ProcessId)
        {
            string sql = "select * from Workstation where 1=1";
            if (!string.IsNullOrEmpty(WorkstationCode))
            {
                sql += " and WorkstationCode like @WorkstationCode";

            }
            if (!string.IsNullOrEmpty(WorkstationName))
            {
                sql += " and WorkstationName like @WorkstationName";

            }
            if (!string.IsNullOrEmpty(WorkshopId))
            {
                sql += " and WorkshopName like @WorkshopName";

            }
            if (!string.IsNullOrEmpty(ProcessId))
            {
                sql += " and ProcessName like @ProcessName";

            }

            sql += " order by CreateTime desc offset @PageSize*(@PageIndex-1) row fetch next @PageSize rows only";

            var list = await Db.QueryAsync<Workstation>(sql, new { PageIndex, PageSize, WorkstationCode = $"%{WorkstationCode}%", WorkstationName = $"%{WorkstationName}%", WorkshopName = $"%{WorkshopId}%", ProcessName = $"%{ProcessId}%" });
            return list;
        }

        public async Task<int> GetCountAsync(string WorkstationCode, string WorkstationName, string WorkshopId, string ProcessId)
        {
            string sql = "select count(*) from Workstation where 1=1 ";
            if (!string.IsNullOrEmpty(WorkstationCode))
            {
                sql += " and WorkstationCode like @WorkstationCode ";
            }

            if (!string.IsNullOrEmpty(WorkstationName))
            {
                sql += " and WorkstationName like @WorkstationName ";
            }
            if (!string.IsNullOrEmpty(WorkshopId))
            {
                sql += " and WorkshopName like @WorkshopName ";
            }
            if (!string.IsNullOrEmpty(ProcessId))
            {
                sql += " and ProcessName like @ProcessName ";
            }

            var count = await Db.GetRecoreCountAsync(sql, new { WorkstationCode = $"%{WorkstationCode}%", WorkstationName = $"%{WorkstationName}%", WorkshopName = $"%{WorkshopId}%", ProcessName = $"%{ProcessId}%" });
            return count;

        }

        public async Task<IEnumerable<WorkstationMachine>> GetWorkstationbyIdAsync(string WorkstationId)
        {
            var list = await Db.QueryAsync<WorkstationMachine>("select  * from WorkstationMachine where WorkstationId = @WorkstationId", new { WorkstationId });
            return list;
        }

        public async Task<bool> UpdateAsync(Workstation model, List<WorkstationMachine> list)
        {
            var result = await Db.UpdateIdentityTranAsync(model,list);
            return result;
        }
    }
}
